// CAudioSourcePage.cpp : implementation file
//

#include "stdafx.h"
#include "avcap.h"
#include "CAudioSourcePage.h"

#include "CLiveCapture.h"
#include "CLiveInputFilters.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAudioSourcePage dialog


CAudioSourcePage::CAudioSourcePage(CWnd* pParent /*=NULL*/)
	: CDialog(CAudioSourcePage::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAudioSourcePage)
	mMixLevel = 0;
	//}}AFX_DATA_INIT
	mLiveCapture = NULL;
}

void CAudioSourcePage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAudioSourcePage)
	DDX_Control(pDX, IDC_SLIDER_MIX_LEVEL, mSliderMixLevel);
	DDX_Control(pDX, IDC_COMBO_AUDIO_CONNECTOR, mComboConnectors);
	DDX_Text(pDX, IDC_EDIT_MIX_LEVEL, mMixLevel);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAudioSourcePage, CDialog)
	//{{AFX_MSG_MAP(CAudioSourcePage)
	ON_WM_HSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAudioSourcePage message handlers
void CAudioSourcePage::SetLiveCapture(CLiveCapture * inData)
{
	mLiveCapture = inData;
}

void CAudioSourcePage::SetInputFilters(CLiveInputFilters * inFilters)
{
	mInputFilters = inFilters;
}

BOOL CAudioSourcePage::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	ASSERT(mLiveCapture);

	FillConnectors();
	InitMixLevelSlider();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CAudioSourcePage::FillConnectors(void)
{
	mComboConnectors.ResetContent();

	if (mInputFilters)
	{
		long count = mInputFilters->GetAudioConnectorCount();
		for (long i = 0; i < count; i++)
		{
			CString connector = mInputFilters->GetAudioConnectorName(i);
			mComboConnectors.AddString(connector);
		}
		
		if (count > 0)
		{
			mComboConnectors.SetCurSel(mLiveCapture->GetAudioConnector());
		}
		else
		{
			mComboConnectors.EnableWindow(FALSE);
		}
	}
}

void CAudioSourcePage::InitMixLevelSlider(void)
{
	mSliderMixLevel.SetRange(0, 100);

	mMixLevel = int(mLiveCapture->GetAudioMixLevel() * 100);
	mSliderMixLevel.SetPos(mMixLevel);
	UpdateData(FALSE);
}

void CAudioSourcePage::EnterConnector(void)
{
	int index = mComboConnectors.GetCurSel();
	if (index != CB_ERR)
	{
		mLiveCapture->SetAudioConnector(index);
	}
}

void CAudioSourcePage::EnterMixLevel(void)
{
	mLiveCapture->SetAudioMixLevel(mMixLevel/100.);
}

void CAudioSourcePage::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	if (pScrollBar->GetSafeHwnd() == mSliderMixLevel.GetSafeHwnd())
	{
		mMixLevel = mSliderMixLevel.GetPos();
		UpdateData(FALSE);
	}

	CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}

void CAudioSourcePage::OnOK() 
{	
	EnterConnector();
	EnterMixLevel();
	
	CDialog::OnOK();
}
